/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */
#include <string.h>
#include <drv/spiflash.h>
#include <yoc/partition_flash.h>
#include <drv/spiflash.h>
#include "systick.h"
#include "spi_flash.h"
// static spiflash_handle_t spiflash_hd;
// static spiflash_info_t *spiflash_info;

static int _boot_flash_info_get(void *handle, partition_flash_info_t *info)
{
    if (info != NULL) {
        info->start_addr = 0x18000000;
        info->sector_size = 0x1000;
        info->sector_count = 128;
        return 0;
    }
    return -1;
}

static int _boot_flash_read(void *handle, uint32_t addr, void *data, size_t data_len)
{
    csi_spiflash_read(handle,addr,data,data_len);
    return 0;
}

static int _boot_flash_write(void *handle, uint32_t addr, void *data, size_t data_len)
{
    csi_spiflash_program(handle,addr,data,data_len);
    return 0;
}

static int _boot_flash_erase(void *handle, uint32_t addr, size_t len)
{
    return csi_spiflash_erase(handle,addr,len);
}

static partition_flash_ops_t g_flash_ops = {
    .open = NULL,
    .close = NULL,
    .info_get = _boot_flash_info_get,
    .read = _boot_flash_read,
    .write = _boot_flash_write,
    .erase = _boot_flash_erase
};

int boot_flash_init(void)
{
    partition_flash_register(&g_flash_ops);
    return 0;
}